import Mock from "mockjs";
//延时200-600毫秒请求到数据
Mock.setup({
  timeout: "200-600",
});

const Random = Mock.Random;
// 用户总览
function countUserNum() {
  const a = Mock.mock({
    success: true,
    data: {
      offlineNum: "@integer(1, 100)",
      lockNum: "@integer(1, 10)",
      totalNum: 218,
    },
  });
  a.data.onlineNum = a.data.totalNum - a.data.offlineNum - a.data.lockNum;
  return a;
}

// 接口，第一个参数url，第二个参数请求类型，第三个参数响应回调
Mock.mock(new RegExp("countUserNum"), "get", countUserNum);

// /设备总览

function countDeviceNum() {
  const a = Mock.mock({
    success: true,
    data: {
      alarmNum: 9800,
      offlineNum: "@integer(4500, 5000)",
      totalNum: 7698,
      onlineNum: "@integer(7000, 9000)",
    },
  });

  return a;
}

Mock.mock(new RegExp("countDeviceNum"), "get", countDeviceNum);

// /设备总览

function sbtx() {
  const a = Mock.mock({
    success: true,
    data: {
      "list|20": [
        {
          provinceName: "@province()",
          cityName: "@city()",
          countyName: "@county()",
          createTime: "@datetime('yyyy-MM-dd HH:mm:ss')",
          deviceId: "6c512d754bbcd6d7cd86abce0e0cac58",
          "gatewayno|+1": 10000,
          "onlineState|1": [0, 1],
        },
      ],
    },
  });
  return a;
}

Mock.mock(new RegExp("sbtx"), "get", sbtx);

//中间地图

function centermap(options) {
  let params = parameteUrl(options.url);
  if (params.regionCode && params.regionCode != "china") {
    const chinaGeoCoordMap = {
      沂水县: [118.634543, 35.787029],
      蒙阴县: [117.943271, 35.712435],
      沂南县: [118.455395, 35.547002],
      平邑县: [117.631884, 35.511519],
      费县: [117.968869, 35.269174],
      兰山区: [118.327667, 35.061631],
      河东区: [118.398296, 35.085004],
      莒南县: [118.838322, 35.175911],
      兰陵县: [118.049968, 34.855573],
      罗庄区: [118.284795, 34.997204],
      临沭县: [118.648379, 34.917062],
      郯城县: [118.342963, 34.614741],
    };
    const a = Mock.mock({
      success: true,
      data: {
        // "dataList|30": [
        //   {
        //     name: "@city()",
        //     value: "@integer(1, 1000)",
        //   },
        // ],
        dataList: [
          { name: "沂水县", value: [118.634543, 35.787029] },
          { name: "蒙阴县", value: [117.943271, 35.712435] },
          { name: "沂南县", value: [118.455395, 35.547002] },
          { name: "平邑县", value: [117.631884, 35.511519] },
          { name: "费县", value: [117.968869, 35.269174] },
          { name: "兰山区", value: [118.327667, 35.061631] },
          { name: "河东区", value: [118.398296, 35.085004] },
          { name: "莒南县", value: [118.838322, 35.175911] },
          { name: "兰陵县", value: [118.049968, 34.855573] },
          { name: "罗庄区", value: [118.284795, 34.997204] },
          { name: "临沭县", value: [118.648379, 34.917062] },
          { name: "郯城县", value: [118.342963, 34.614741] },
          // { name: "沂水县", value: 100 },
          // { name: "蒙阴县", value: 100 },
          // { name: "沂南县", value: 100 },
          // { name: "平邑县", value: 100 },
          // { name: "费县", value: 100 },
          // { name: "兰山区", value: 100 },
          // { name: "河东区", value: 100 },
          // { name: "莒南县", value: 100 },
          // { name: "兰陵县", value: 100 },
          // { name: "罗庄区", value: 100 },
          // { name: "临沭县", value: 100 },
          // { name: "郯城县", value: 100 },
        ],
        regionCode: params.regionCode, //-代表中国
      },
    });
    return a;
  } else {
    const chinaGeoCoordMap = {
      黑龙江: [127.9688, 45.368],
      内蒙古: [110.3467, 41.4899],
      吉林: [125.8154, 44.2584],
      北京市: [116.4551, 40.2539],
      辽宁: [123.1238, 42.1216],
      河北: [114.4995, 38.1006],
      天津: [117.4219, 39.4189],
      山西: [112.3352, 37.9413],
      陕西: [109.1162, 34.2004],
      甘肃: [103.5901, 36.3043],
      宁夏: [106.3586, 38.1775],
      青海: [101.4038, 36.8207],
      新疆: [87.611053, 43.828171],
      西藏: [91.117212, 29.646922],
      四川: [103.9526, 30.7617],
      重庆: [108.384366, 30.439702],
      山东: [117.1582, 36.8701],
      河南: [113.4668, 34.6234],
      江苏: [118.8062, 31.9208],
      安徽: [117.29, 32.0581],
      湖北: [114.3896, 30.6628],
      浙江: [119.5313, 29.8773],
      福建: [119.4543, 25.9222],
      江西: [116.0046, 28.6633],
      湖南: [113.0823, 28.2568],
      贵州: [106.6992, 26.7682],
      云南: [102.9199, 25.4663],
      广东: [113.12244, 23.009505],
      广西: [108.479, 23.1152],
      海南: [110.3893, 19.8516],
      台湾: [120.702967, 24.123621],
      上海: [121.4648, 31.2891],
    };
    const chinaDatas = [
      [
        {
          name: "北京市",
          value: 0,
        },
        {
          name: "黑龙江",
          value: 0,
        },
        { name: "上海市" },
      ],
      [
        {
          name: "内蒙古",
          value: 0,
        },
      ],
      [
        {
          name: "吉林",
          value: 0,
        },
      ],
      [
        {
          name: "辽宁",
          value: 0,
        },
      ],
      [
        {
          name: "河北",
          value: 0,
        },
      ],
      [
        {
          name: "天津",
          value: 0,
        },
      ],
      [
        {
          name: "山西",
          value: 0,
        },
      ],
      [
        {
          name: "陕西",
          value: 0,
        },
      ],
      [
        {
          name: "甘肃",
          value: 0,
        },
      ],
      [
        {
          name: "新疆",
          value: 0,
        },
      ],
      [
        {
          name: "西藏",
          value: 0,
        },
      ],
      [
        {
          name: "台湾",
          value: 0,
        },
      ],
      [
        {
          name: "黑龙江",
          value: 0,
        },
      ],
      [
        {
          name: "云南",
          value: 0,
        },
      ],
      [
        {
          name: "宁夏",
          value: 0,
        },
      ],
      [
        {
          name: "青海",
          value: 0,
        },
      ],
      [
        {
          name: "四川",
          value: 0,
        },
      ],
      [
        {
          name: "重庆",
          value: 0,
        },
      ],
      [
        {
          name: "山东",
          value: 0,
        },
      ],
      [
        {
          name: "河南",
          value: 0,
        },
      ],
      [
        {
          name: "江苏",
          value: 0,
        },
      ],
      [
        {
          name: "安徽",
          value: 0,
        },
      ],
      [
        {
          name: "湖北",
          value: 0,
        },
      ],
      [
        {
          name: "浙江",
          value: 0,
        },
      ],
      [
        {
          name: "福建",
          value: 0,
        },
      ],
      [
        {
          name: "江西",
          value: 0,
        },
      ],
      [
        {
          name: "湖南",
          value: 0,
        },
      ],
      [
        {
          name: "贵州",
          value: 0,
        },
      ],
      [
        {
          name: "广西",
          value: 0,
        },
      ],
      [
        {
          name: "海南",
          value: 0,
        },
      ],
      [
        {
          name: "上海",
          value: 0,
        },
      ],
    ];

    const convertData = function (data) {
      var res = [];
      for (var i = 0; i < data.length; i++) {
        var dataItem = data[i];
        var fromCoord = chinaGeoCoordMap[dataItem[0].name];
        var toCoord = [
          [121.4648, 31.2891],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
          [117.1582, 36.8701],
        ]; //被攻击点
        if (fromCoord && toCoord[i]) {
          res.push([
            {
              coord: toCoord[i],
            },
            {
              coord: fromCoord,
              value: dataItem[0].value,
              // visualMap: false
            },
          ]);
        }
      }
      return res;
    };

    const dataList = {};
    [["山东", chinaDatas]].forEach(function (item, i) {
      dataList.A = convertData(item[1]);
      dataList.B = item[1].map(function (dataItem) {
        return {
          name: dataItem[0].name,
          value: chinaGeoCoordMap[dataItem[0].name].concat([dataItem[0].value]),
          // visualMap: false
        };
      });
      dataList.C = [
        {
          name: item[0],
          value: chinaGeoCoordMap[item[0]].concat([10]),
        },
      ];
    });
    const a = Mock.mock({
      success: true,
      data: {
        // "dataList|8": [
        //   {
        //     name: "@province()",
        //     value: "@integer(1, 1000)",
        //   },
        // ],
        dataList,
        regionCode: "china",
      },
    });
    return a;
  }
}

Mock.mock(new RegExp("centermap"), "get", centermap);

// 报警次数

function alarmNum() {
  const a = Mock.mock({
    success: true,
    data: {
      dateList: ["1点", "3点", "5点", "7点", "9点", "11点"],
      "numList|6": ["@integer(0, 1000)"],
      "numList2|6": ["@integer(0, 1000)"],
    },
  });
  return a;
}
Mock.mock(new RegExp("alarmNum"), "get", alarmNum);

// 实时预警

function ssyj() {
  const a = Mock.mock({
    success: true,
    data: {
      "list|40": [
        {
          alertdetail: "@csentence(5,10)",
          "alertname|1": ["水浸告警", "各种报警"],
          alertvalue: "@float(60, 200)",
          createtime: "2022-04-19 08:38:33",
          deviceid: null,
          "gatewayno|+1": 10000,
          phase: "A1",
          sbInfo: "@csentence(10,18)",
          "terminalno|+1": 100,
          provinceName: "@province()",
          cityName: "@city()",
          countyName: "@county()",
        },
      ],
    },
  });
  return a;
}
Mock.mock(new RegExp("ssyj"), "get", ssyj);
//安装计划
function installationPlan() {
  let num = RandomNumBoth(26, 32);
  const a = Mock.mock({
    ["category|" + num]: ["@city()"],
    ["barData|" + num]: ["@integer(10, 100)"],
  });
  let lineData = [],
    rateData = [];
  for (let index = 0; index < num; index++) {
    let lineNum = Mock.mock("@integer(0, 100)") + a.barData[index];
    lineData.push(lineNum);
    let rate = a.barData[index] / lineNum;
    rateData.push((rate * 100).toFixed(0));
  }
  a.lineData = lineData;
  a.rateData = rateData;
  return {
    success: true,
    data: a,
  };
}
Mock.mock(new RegExp("installationPlan"), "get", installationPlan);

//报警排名
function ranking() {
  //多生成几个避免重复 重复会报错
  let num = Mock.mock({
    "list|48": [{ value: "@integer(50,1000)", name: "@city()" }],
  }).list;
  //   console.log(num);
  let newNum = [],
    numObj = {};
  num.map((item) => {
    if (!numObj[item.name] && newNum.length < 8) {
      numObj[item.name] = true;
      newNum.push(item);
    }
  });
  let arr = newNum.sort((a, b) => {
    return b.value - a.value;
  });
  let a = {
    success: true,
    data: arr,
  };
  return a;
}
Mock.mock(new RegExp("ranking"), "get", ranking);
/**
 * @description: min ≤ r ≤ max  随机数
 * @param {*} Min
 * @param {*} Max
 * @return {*}
 */
function RandomNumBoth(Min, Max) {
  var Range = Max - Min;
  var Rand = Math.random();
  var num = Min + Math.round(Rand * Range); //四舍五入
  return num;
}
/**
 * @description: 获取路径参数
 * @param {*} url
 * @return {*}
 */
function parameteUrl(url) {
  var json = {};
  if (/\?/.test(url)) {
    var urlString = url.substring(url.indexOf("?") + 1);
    var urlArray = urlString.split("&");
    for (var i = 0; i < urlArray.length; i++) {
      var urlItem = urlArray[i];
      var item = urlItem.split("=");
      console.log(item);
      json[item[0]] = item[1];
    }
    return json;
  }
  return {};
}
